package com.eurasia.assignment.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eurasia.assignment.controller.dto.CreateCodeSubmissionResponse;
import com.eurasia.assignment.controller.dto.JudgeSubmitResponse;
import com.eurasia.assignment.controller.dto.JudgeSubmitSearchRequest;
import com.eurasia.assignment.controller.dto.PageResponse;
import com.eurasia.assignment.controller.dto.ProblemQuery;
import com.eurasia.assignment.domain.hoj.CodeInfo;
import com.eurasia.assignment.service.HOJFullCaseJudgementService;
import com.eurasia.assignment.service.HOJResourceService;
import com.eurasia.assignment.service.HOJSubmissionService;
import com.eurasia.assignment.service.port.out.dto.CreateFullCaseJudgementRequest;
import com.eurasia.assignment.service.port.out.dto.CreateFullCaseJudgementResponse;
import com.eurasia.assignment.service.port.out.dto.HOJResource;
import com.fasterxml.jackson.core.JsonProcessingException;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/hoj")
@RequiredArgsConstructor
public class HOJResourceController {
    private static final Logger log = LoggerFactory.getLogger(HOJResourceController.class);
    
    private final HOJResourceService service;
    private final HOJFullCaseJudgementService fullCaseJudgementService;
    private final HOJSubmissionService submissionService;

    @GetMapping("problems")
    public ResponseEntity<String> getProblems(@SpringQueryMap ProblemQuery query) {
        return service.getProblems(query);
    }

    @GetMapping("problems/{id}")
    public ResponseEntity<String> getProblemDetail(@PathVariable String id) {
        return service.getProblemById(id);
    }

    @PostMapping("test-judgement")
    public ResponseEntity<CreateCodeSubmissionResponse> createCodeSubmission(@RequestBody CodeInfo request) throws JsonProcessingException {
        return service.createSingleCaseJudgement(request);
    }

    @GetMapping("test-judgement-result")
    public ResponseEntity<String> getTestJudgementResult(@RequestParam String testJudgeKey) {
        return service.getJudgementResult(testJudgeKey);
    }

    @PostMapping("full-case-judgements")
    public ResponseEntity<HOJResource<CreateFullCaseJudgementResponse>> getTestJudgementResult(@RequestBody CreateFullCaseJudgementRequest request) {
        return service.createFullCaseJudgement(request);
    }

    @GetMapping("full-case-judgement-result")
    public ResponseEntity<String> getFullCaseJudgementResult(@RequestParam Long submitId) {
        return fullCaseJudgementService.fetchFullCaseJudgementResult(submitId);
    }

    @GetMapping("full-case-judgement-detailed-result")
    public ResponseEntity<String> getFullCaseJudgementDetailedResult(@RequestParam Long submitId) {
        return fullCaseJudgementService.fetchFullCaseJudgementDetailedResult(submitId);
    }
    
    /**
     * 获取当前登录用户的判题提交记录
     * 
     * @param status 判题状态
     * @param language 编程语言
     * @param page 页码，默认为0
     * @param size 每页大小，默认为10
     * @param sortBy 排序字段，默认为createdAt
     * @param sortDirection 排序方向，默认为desc
     * @return 分页后的提交记录列表
     */
    @GetMapping("me/judge-submits")
    public ResponseEntity<PageResponse<JudgeSubmitResponse>> getCurrentUserSubmissions(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String language,
            @RequestParam(required = false, defaultValue = "0") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false, defaultValue = "createdAt") String sortBy,
            @RequestParam(required = false, defaultValue = "desc") String sortDirection) {
        
        log.debug("收到获取用户提交记录请求: status={}, language={}, page={}, size={}, sortBy={}, sortDirection={}",
                status, language, page, size, sortBy, sortDirection);
        
        // 构建查询请求
        JudgeSubmitSearchRequest searchRequest = new JudgeSubmitSearchRequest();
        searchRequest.setStatus(status);
        searchRequest.setLanguage(language);
        searchRequest.setPage(page);
        searchRequest.setSize(size);
        searchRequest.setSortBy(sortBy);
        searchRequest.setSortDirection(sortDirection);
        
        // 执行查询
        Page<JudgeSubmitResponse> responsePage = submissionService.getCurrentUserSubmissions(searchRequest)
                .map(JudgeSubmitResponse::fromDomain);
        
        // 构建响应
        return ResponseEntity.ok(PageResponse.from(responsePage));
    }
}
